package com.jbm.core.common;

import com.jbm.core.iface.IAcross;
import com.jbm.core.server.SAccount;

import org.apache.mina.core.session.IoSession;

import java.util.HashMap;

/**
 * Created by root on 17-3-5.
 */
public class AccountCtrl
{
	public static final Integer Account_Server_id = 0xFFFF0001;
	public static final AccountCtrl g_accountctrl = new AccountCtrl();

	protected Resource m_accountqueue;

	protected final Object m_accmap_mutex = new Object();
	protected HashMap<Integer,IAccount> m_accountmap;

	protected Resource m_caqueue;

	public AccountCtrl()
	{
	}

	public void InitAccountqueue(int rescnt)
	{
		m_accountqueue = new Resource(rescnt,"account");
	}

	public void InitAccountmap()
	{
		m_accountmap = new HashMap<Integer,IAccount>();
	}

	public void InitCAqueue(int rescnt)
	{
		m_caqueue = new Resource(rescnt,"connectagent");
	}

	public void AddCA(ConnectAgent ca)
	{
		m_caqueue.Add(ca);
	}

	public ConnectAgent GetorAddCA(String ca_key, ConnectAgent ca, boolean doadd)
	{
		return (ConnectAgent)m_caqueue.GetorAdd(null,null,doadd,ca,ca_key);
	}

	public ConnectAgent GetCA(Integer account_id, String app_type)
	{
		IAccount ia = null;

		synchronized (m_accmap_mutex)
		{
			ia = (IAccount) m_accountmap.get(account_id);
		}

		if ( ia == null )
			return null;

		return ia.GetCA(app_type);
	}

	public IoSession GetIoSession(Integer account_id, String app_type)
	{
		ConnectAgent ca = GetCA(account_id, app_type);

		if (ca == null)
			return null;

		return ca.getM_iosession();
	}

	public SAccount GetSA(Integer account_id )
	{
		return (SAccount) m_accountqueue.Get(null,null,account_id);
	}

	public void AddSA(SAccount sa)
	{
		m_accountqueue.Add(sa);
	}

	public void NewSA(SAccount sa)
	{
	}

	public boolean Connect2Point(Integer account_id, String app_type )
	{
		ConnectAgent ca = GetCA(account_id,app_type);

		if ( ca == null )
			return false;

		return ca.ConnectP2P(true);
	}

	public boolean Connect2Server(Integer account_id, String app_type )
	{
		ConnectAgent ca = GetCA(account_id,app_type);

		if ( ca == null )
			return false;

		return ca.ConnectServer();
	}

	public IAcross SendPoint (Integer to_id, String app_type, String classname, String method, IAcross content, boolean hasresp, boolean waitresp )
	{
		ConnectAgent ca = GetCA(to_id,app_type);

		if ( ca == null )
			ca = GetCA(Account_Server_id,"app_server");

		return ca.Send(to_id,app_type,classname,method,content,hasresp,waitresp,ConnectAgent.Send_Type_Any,null);
	}

	public void PushPoint (Integer account_id, ConnectAgent ca )
	{
		IAccount ia = null;

		synchronized (m_accmap_mutex)
		{
			ia = m_accountmap.get(account_id);
		}

		if ( ia == null )
		{
			ia = new IAccount(account_id);

			synchronized (m_accmap_mutex)
			{
				m_accountmap.put(account_id, ia);
			}
		}
/*
		else if ( ia.... != account.... )
			//call callback;
*/
		ia.Push2Map(ca,MainServer.g_server.getM_maincallback());

	}

	public void OfflinePoint (Integer account_id, ConnectAgent ca )
	{
		IAccount ia = null;

		synchronized (m_accmap_mutex)
		{
			ia = m_accountmap.get(account_id);
		}

		if ( ia != null )
		{
			ia.Offline2Map(ca,MainServer.g_server.getM_maincallback());
		}
	}
}
